# _*_ coding ：utf-8 _*_
# @Time : 2023/9/22 8:24
# @Author : 计科1914-02-陈鸿宇
# @File : img
# @Project : 作业
import re
import urllib.request

import requests
from bs4 import BeautifulSoup


def img(num):
    res = urllib.request.urlopen("https://www.nipic.com/topic/show_27313_1.html")
    html = res.read().decode("utf-8")

    pattern = re.compile(r"""<img[^>]+src=["']([^'"<>]+)["'][^<>]+/?>""", re.S)
    str2 = pattern.findall(html)

    if int(num) > len(str2):
        print("下载图片数量过多！")
        return

    for i in range(int(num)):
        dowlimg = requests.get("https:" + str2[i])
        open('图片\\' + "第" + str(i + 1) + '张图片' + '.png', 'wb').write(dowlimg.content)

    print("下载完成")


if __name__ == '__main__':
    img(input("请输入要下载的数量:"))
